//
//  DKTabBarViewController.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/26.
//

#import "DKTabBarViewController.h"
#import "DKAuthorizeHomeViewController.h"
#import "DKUnauthorizeHomeViewController.h"
#import "DKAuthorizeProductViewController.h"
#import "DKUnauthorizeProductViewController.h"
//#import "DKAuthorizeMainViewController.h"
#import "DKUnauthorizeMainViewController.h"
#import "DKBaseNavigationController.h"
@interface DKTabBarViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong)NSMutableArray * dk_tabbars;
@end

@implementation DKTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
    [self makeTabbar];
}
- (void)makeTabbar{
    if([[DKGlobalHelper defaultManager].loanSetpInfoMdl.step isEqualToString:@"100"]){
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarHome" withCtrl:[[DKAuthorizeHomeViewController alloc] init]];
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarProduct" withCtrl:[[DKUnauthorizeProductViewController alloc] init]];
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarMain" withCtrl:[[DKUnauthorizeMainViewController alloc] init]];
    }else{
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarHome" withCtrl:[[DKUnauthorizeHomeViewController alloc] init]];
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarProduct" withCtrl:[[DKUnauthorizeProductViewController alloc] init]];
        [self makeSubTabbar:@"" withImgName:@"dk_tabbarMain" withCtrl:[[DKUnauthorizeMainViewController alloc] init]];
    }
}
- (void)makeSubTabbar:(NSString *)title withImgName:(NSString *)imageName withCtrl:(UIViewController *)tabBarCtrl{
    NSString * namorImg = [NSString stringWithFormat:@"%@Normal",imageName];
    NSString * selectImg = [NSString stringWithFormat:@"%@Select",imageName];
    DKBaseNavigationController * navTabCtrl = [[DKBaseNavigationController alloc] initWithRootViewController:tabBarCtrl];
    //    navTabCtrl.tabBarItem.title = title;
    navTabCtrl.tabBarItem.image = [DK_GetImage(namorImg) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navTabCtrl.tabBarItem.selectedImage = [DK_GetImage(selectImg) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:navTabCtrl];
}
- (void)addScaleAnimationOnView:(UIView *)animationView repeatCount:(float)repeatCount {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@1.0,@1.2,@0.8,@1.1,@0.9,@1.0];
    animation.duration = 1;
    animation.repeatCount = repeatCount;
    animation.calculationMode = kCAAnimationCubic;
    [animationView.layer addAnimation:animation forKey:nil];
}
#pragma mark UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if(!self.dk_tabbars){
        self.dk_tabbars = [[NSMutableArray alloc] init];
        UITabBar *tabBar = tabBarController.tabBar;
        for (UIView *tabBarBtn in tabBar.subviews) {
            if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [self.dk_tabbars addObject:tabBarBtn];
            }
        }
    }
    if(self.dk_tabbars.count > 0){
        UIView* selectTabbarBtn = self.dk_tabbars[tabBarController.selectedIndex];
        for (UIImageView* imgaView in selectTabbarBtn.subviews) {
            if ([imgaView isKindOfClass:[UIImageView class]]) {
                [self addScaleAnimationOnView:imgaView repeatCount:1];
            }
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
